package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.ExcelDAO;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentExcel;
import cn.edu.nju.software.greencreditsystem.domain.FinancialExcel;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-15
 * 类描述：报表Excel类DAO实现
 */
public class ExcelDAOImpl implements ExcelDAO
{

    @Override
    public List<FinancialExcel> getCurrentFinancial(int enterpriseID)
    {
        List<FinancialExcel> result = new ArrayList<FinancialExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<FinancialExcel> temp= en.getFinancialExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today))
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentExcel> getCurrentEnvironment(int enterpriseID)
    {
        List<EnvironmentExcel> result = new ArrayList<EnvironmentExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> temp= en.getEnvironmentExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today))
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialExcel> getFinancialUnchecked(int enterpriseID)
    {
        List<FinancialExcel> result = new ArrayList<FinancialExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<FinancialExcel> temp= en.getFinancialExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today) && temp.get(i).getChecked() == false && temp.get(i).getData() != null)
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentExcel> getEnvironmentUnchecked(int enterpriseID)
    {
        List<EnvironmentExcel> result = new ArrayList<EnvironmentExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> temp= en.getEnvironmentExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today) && temp.get(i).getChecked() == false && temp.get(i).getData() != null)
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean save(FinancialExcel fe, int enterpriseID)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<FinancialExcel> temp= en.getFinancialExcel();
            temp.add(fe);
            s.update(en);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean save(EnvironmentExcel ee, int enterpriseID)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> temp= en.getEnvironmentExcel();
            temp.add(ee);
            s.update(en);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(FinancialExcel fe, int enterpriseID)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<FinancialExcel> temp= en.getFinancialExcel();
            for(FinancialExcel et:temp)
            {
                if(et.getId() == fe.getId())
                {
                    temp.remove(et);
                    break;
                }
            }
            s.update(en);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(EnvironmentExcel ee, int enterpriseID)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> temp= en.getEnvironmentExcel();
            for(EnvironmentExcel et:temp)
            {
                if(et.getId() == ee.getId())
                {
                    temp.remove(et);
                    break;
                }
            }
            s.update(en);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialExcel getFinancialExcel(int financialID)
    {
        FinancialExcel result = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (FinancialExcel) s.get(FinancialExcel.class, financialID);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnvironmentExcel getEnvironmentExcel(int environmentID)
    {
        EnvironmentExcel result = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (EnvironmentExcel) s.get(EnvironmentExcel.class, environmentID);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(FinancialExcel fe)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(fe);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(EnvironmentExcel ee)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(ee);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialExcel> getFinancialChecked(int enterpriseID)
    {
        List<FinancialExcel> result = new ArrayList<FinancialExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<FinancialExcel> temp= en.getFinancialExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today) && temp.get(i).getChecked() == true)
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentExcel> getEnvironmentChecked(int enterpriseID)
    {
        List<EnvironmentExcel> result = new ArrayList<EnvironmentExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.load(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> temp= en.getEnvironmentExcel();
            Date today = new Date(System.currentTimeMillis());
            for(int i = temp.size() - 1; i >= 0; i--)
            {
                if(temp.get(i).getBeginDate().before(today) && temp.get(i).getChecked() == true)
                    result.add(temp.get(i));
            }
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialExcel> getFinancialUnchecked()
    {
        List<FinancialExcel> result = new ArrayList<FinancialExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialExcel "
                                     + "where checked = false "
                                     + "and beginDate < :today "
                                     + "and submitDate != null");
            q.setDate("today", new Date(System.currentTimeMillis()));
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<EnvironmentExcel> getEnvironmentUnchecked()
    {
        List<EnvironmentExcel> result = new ArrayList<EnvironmentExcel>();
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnvironmentExcel "
                                     + "where checked = false "
                                     + "and beginDate < :today "
                                     + "and submitDate != null");
            q.setDate("today", new Date(System.currentTimeMillis()));
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public int nextEnvironment(int enterpriseID)
    {
        int result = 0;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.get(Enterprise.class, enterpriseID);
            List<EnvironmentExcel> list = en.getEnvironmentExcel();
            EnvironmentExcel next = null;
            Date today = new Date(System.currentTimeMillis());
            for(int i = 0; i < list.size(); i++)
            {
                if(list.get(i).getSubmitDate() == null
                        && list.get(i).getDeadline().after(today)
                        && (next == null || list.get(i).getDeadline().before(next.getDeadline())))
                    next = list.get(i);
            }
            if(next != null)
                result = (int) ((next.getDeadline().getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public int nextFinancial(int enterpriseID)
    {
        int result = 0;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Enterprise en = (Enterprise) s.get(Enterprise.class, enterpriseID);
            List<FinancialExcel> list = en.getFinancialExcel();
            FinancialExcel next = null;
            Date today = new Date(System.currentTimeMillis());
            for(int i = 0; i < list.size(); i++)
            {
                if(list.get(i).getSubmitDate() == null
                        && list.get(i).getDeadline().after(today)
                        && (next == null || list.get(i).getDeadline().before(next.getDeadline())))
                    next = list.get(i);
            }
            if(next != null)
                result = (int) ((next.getDeadline().getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }
}
